import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import Node from './modules/node'
import Vm from './modules/vm'
import User from './modules/user'
import Sku from './modules/sku'
import Policy from './modules/policy'
import Order from './modules/order'
import Report from './modules/report'
export const constantRoutes = [
  // 登录页面
  {
    path: '/login',
    component: () => import('@/views/Login/index'),
    hidden: true
  },
  // 404 页面
  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 首页
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [{
      path: 'home',
      name: 'Home',
      component: () => import('@/views/Home/index'),
      meta: { title: '帝可得', icon: 'home' }
    }]
  },
  // 工资管理页面
  {
    path: '/task',
    component: Layout,
    redirect: '/task/business',
    name: 'Task',
    meta: { title: '工单管理', icon: 'jsb' },
    children: [{
      path: 'business',
      name: 'business',
      component: () => import('@/views/Task/business'),
      meta: { title: '运营工单' }
    },
    {
      path: 'operation',
      name: 'operation',
      component: () => import('@/views/Task/operation'),
      meta: { title: '运维工单' }
    }]
  },
  Node,
  Vm,
  User,
  Sku,
  Policy,
  Order,
  Report
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// reset 路由方法
export function resetRouter() {
  // 得到一个全新的 router 实例对象
  const newRouter = createRouter()
  // 使用新的路由记录覆盖掉老的路由记录
  router.matcher = newRouter.matcher // reset router
}

export default router
